import java.util.ArrayList;

/**
 * Created by China Wang on 5/28/14.
 */
public class PrimeFactors {

    public static void main(String[] args) {
        ArrayList<Integer> result = generate(1);
        printList(result);
        result = generate(30);
        printList(result);
    }

    public static ArrayList<Integer> generate(int n) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                if (isPrime(i)) {
                    list.add(i);
                }
            }
        }
        if (isPrime(n)) {
            list.add(n);
        }
        return list;
    }

    private static boolean isPrime(int n) {
        if (n == 1) return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    private static void printList(ArrayList<Integer> list) {
        if (list.isEmpty()) {
            System.out.println("List is empty");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (i != list.size() - 1)
                System.out.print(list.get(i) + ", ");
            else
                System.out.println(list.get(i));
        }
    }

}
